﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Competition.Admin.Web.Models.BannerViewModels;
using Competition.Admin.Web.ResultModels;
using Competition.Admin.Web.ResultModels.BannerResultModels;
using Competition.Application.DTO;
using Competition.Application.Interfaces;
using Competition.Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Competition.Admin.Web.Controllers
{
    /// <summary>
    /// 轮播图控制器
    /// </summary>
    public class BannerController : Controller
    {
        private readonly Lazy<IBannerService> _bannerService;  //轮播图业务

        /// <summary>
        /// 构造函数
        /// </summary>
        public BannerController(Lazy<IBannerService> bannerService)
        {
            _bannerService = bannerService;
        }

        // GET: Banner
        public ActionResult Index()
        {
            return View();
        }

        // GET: Banner/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Banner/Create
        public ActionResult Create()
        {
            return View(nameof(Create));
        }

        // POST: Banner/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([FromForm]BannerViewModel model, [FromForm]IFormFile imgFile)
        {
            if (imgFile == null)
            {
                ModelState.AddModelError(nameof(model.Url), "必填");
            }

            if (ModelState.IsValid)
            {
                MessageBase result = new MessageBase();
                BannerDto dto = new BannerDto()
                {
                    Link = model.Link,
                    Status = model.Status,
                    Title = model.Title,
                    Url = UploadHelper.UploadImage(imgFile),
                };
                int id = await _bannerService.Value.AddAsync(dto);
                result.Success = id > 0;
                return Json(result);
            }

            return View(nameof(Create), model);
        }

        // GET: Banner/Edit/5
        public async Task<ActionResult> Edit(int id)
        {
            BannerDto dto = await _bannerService.Value.FindAsync(id);
            BannerViewModel model = new BannerViewModel()
            {
                Id = dto.Id,
                Title = dto.Title,
                Link = dto.Link,
                Url = dto.Url,
                Status = dto.Status
            };
            return View(nameof(Edit), model);
        }

        // POST: Banner/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit(int id, [FromForm]BannerViewModel model, [FromForm]IFormFile imgFile)
        {
            if (ModelState.IsValid)
            {
                MessageBase result = new MessageBase();
                var dto = new BannerDto()
                {
                    Id = id,
                    Title = model.Title,
                    Link = model.Link,
                    Url = imgFile == null ? model.Url : UploadHelper.UploadImage(imgFile),
                    Status = model.Status
                };

                result.Success = await _bannerService.Value.UpdateAsync(dto);
                return Json(result);
            }

            return View(nameof(Edit), model);
        }

        // POST: Banner/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Delete(int id)
        {
            MessageBase result = new MessageBase();
            result.Success = await _bannerService.Value.DeleteAsync(id);
            return Json(result);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="Checked">是否有效</param>
        /// <returns></returns>
        // POST: News/Lock/5
        public async Task<IActionResult> Lock(int id, String Checked)
        {
            MessageBase result = new MessageBase();
            int status = Checked == "false" ? 0 : 1;
            result.Success = await _bannerService.Value.UpdateStatusAsync(id, status);
            return Json(result);
        }

        /// <summary>
        /// 根据条件查找新闻列表
        /// </summary>
        /// <param name="page">页面索引</param>
        /// <param name="limit">页面大小</param>
        /// <param name="terminalType">终端类型，1-APP，2-PC网站</param>
        /// <param name="title">标题</param>
        /// <returns>新闻列表</returns>
        [HttpGet, ActionName(nameof(DataJson))]
        public async Task<IActionResult> DataJson(int page, int limit)
        {
            var v = await _bannerService.Value.FindListAsync(page, limit);
            var model = v.dtos.Select(dto => new BannerResultModel()
            {
                Id = dto.Id,
                AddTime = dto.AddTime.ToString("yyyy-MM-dd HH:mm:ss"),
                Title = dto.Title,
                Status = dto.Status,
            }).ToList();

            var result = new
            {
                Code = 0,
                Msg = "",
                Count = v.pageDto.TotalRecord,
                Data = model
            };
            return new JsonResult(result);
        }
    }
}